package com.coollang.smartbra.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.andexert.library.RippleView;
import com.coollang.smartbra.MyAutoLayoutActivity;
import com.coollang.smartbra.R;
import com.coollang.smartbra.constant.Constants;
import com.coollang.smartbra.eventbus.CommonEvent;
import com.coollang.smartbra.utils.BaseTextWatcher;
import com.coollang.smartbra.utils.ToastUtils;
import com.coollang.smartbra.utils.TranslucentSystemBarUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

/**
 * 修改名字
 * Created by Administrator on 2016/11/10 0010.
 */

public class EditNameActivity extends MyAutoLayoutActivity {
    @BindView(R.id.status_bar)
    View statusBar;
    @BindView(R.id.tv_calendar_realtime)
    TextView tvLeft;
    @BindView(R.id.tv_title_realtime)
    TextView tvTitle;
    @BindView(R.id.content)
    LinearLayout content;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.delete_all)
    ImageView deleteAll;
    String stringExtra;
    boolean editDeviceName;
    @BindView(R.id.rv_back)
    RippleView rvBack;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_editname);
        ButterKnife.bind(this);
        TranslucentSystemBarUtils.showChenJinZhuangTaiLan(statusBar, this, content);
        initViews();
        initBuandle();
    }

    private void initBuandle() {
        stringExtra = getIntent().getStringExtra(Constants.EDITNMAE_TEXT);
        editDeviceName = getIntent().getBooleanExtra("editDeviceName", false);
        if (editDeviceName) {
            tvTitle.setText(getString(R.string.edit_name));
        } else {
            tvTitle.setText(getString(R.string.person_name));
        }
        if (!stringExtra.isEmpty()) {
            etName.setText(stringExtra);
            etName.setSelection(stringExtra.length());//将光标移至文字末尾
            etName.requestFocus();
            etName.addTextChangedListener(new BaseTextWatcher() {
                @Override
                public void afterTextChanged(Editable s) {
                    if (s.length() == 0) {
                        deleteAll.setVisibility(View.INVISIBLE);
                    } else {
                        deleteAll.setVisibility(View.VISIBLE);
                    }
                }
            });
        }
    }

    private void initViews() {
        rvBack.setVisibility(View.GONE);
        tvLeft.setText(getString(R.string.cancel));
        tvRight.setText(getString(R.string.ok));
        tvRight.setTextColor(getResources().getColor(R.color.progrss_bg));
        tvTitle.setTextColor(getResources().getColor(R.color.black_80));
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.tv_calendar_realtime, R.id.tv_right, R.id.delete_all})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_calendar_realtime:
                finish();
                break;
            case R.id.tv_right:
                //修改名字网络请求
                if ("".equals(etName.getText() + "") || etName.getText().toString().trim().isEmpty()) {
                    ToastUtils.showToast(getString(R.string.no_edit));
                } else if (etName.getText().length() > 10) {
                    ToastUtils.showToast(getString(R.string.edit_10));
                } else if (stringExtra.equals(etName.getText() + "")) {
                    finish();
                } else {
                    if (editDeviceName) {
                        //BleManager.getInstance().sendNameData(etName.getText().toString().trim() + "");
                    } else {
                        EventBus.getDefault().post(new CommonEvent(etName.getText().toString().trim() + "", CommonEvent.REQUESTSUEECCD, CommonEvent.EDITNAME));
                    }
                    finish();
                }
                break;
            case R.id.delete_all:
                if (!"".equals(etName.getText())) {
                    etName.setText("");
                    etName.requestFocus();
                    InputMethodManager imm = (InputMethodManager) etName.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
                } else {

                }
                break;
        }
    }

}
